Installation Guide

Author

Prof. Matthew G. Son

Published

February 5, 2025

1 Foreword

For the class, you should install R on your computer. For IDE, installing either RStudio or Positron IDE will be sufficient, and there’s no need to install both. Section 2 will be mostly relevant before coming to the first class.

1.1 What is IDE?

Integrated Development Environment (IDE), provides a user-friendly interface and convenient features for programming. Positron, RStudio, VSCode, PyCharm, Atom, Spyder, and Jupyter Lab are popular ones.

Positron IDE is a new code editor designed for both R & Python in the data science field. It is built on top of VSCode IDE and offers basic functionality of VSCode and seamless integration with R and Python. Currently open beta.

RStudio provides support for Python (with reticulate package), allowing users to effortlessly navigate between both languages.

1.2 R language

R is widely recognized and utilized by various professionals in fields such as data science, statistics, econometrics, and quantitative/financial analysis. Its popularity stems from its powerful capabilities and extensive range of packages for statistical analysis, big data analysis, visualization, and machine learning. I think (modern) R syntax is more streamlined, consistent and convenient for financial research.

1.3 Python language

Python is widely recognized and used in fields like computer science for general purpose and very popular in data science. Its popularity comes from its versatility, and a vast array of libraries that support tasks from data engineering to artificial intelligence - currently a lingua franca for machine learning.

2 Installations before the first class

If you are new to programming, please install R and IDE on your computer before coming to the first class. I’ll guide you to install python later in the course if necessary.

2.1 Install R

Let’s download and install R.

  1. Navigate to the R CRAN webpage to download R client. Please download most recent R client that matches your OS.

R CRAN Homepage

2.1.1 Windows

  • For Windows, choose ‘base’ sub-directories to download R.

Choose base for Windows

It is recommended that users adhere to the default settings during installation unless they are experienced with environment variable settings (path). Modifying these settings without sufficient familiarity can potentially lead to time-consuming issues or complications.

Windows R installation

2.1.2 MacOS

For Mac, choose R-4.x.x.pkg file. Make sure to download proper build for your mac (Intel or Apple silicon(M1, M2, etc)).

Choose Intel (X64) or Apple Silicon (Arm64)

It is recommended that users adhere to the default settings during installation unless they are experienced with environment variable settings (path). Modifying these settings without sufficient familiarity can potentially lead to time-consuming issues or complications.

For macOS, install R package by executing .pkg file.

Mac R installation

2.2 Install RStudio IDE

Navigate to RStudioIDE Stable Build to get the stable version of RStudio IDE. Download installers that matches your OS from RStudio Desktop section.

RStudio Desktop

2.2.1 RStudio Installation - Windows

The installation process of RStudio on Windows is typically straightforward.

If you have successfully installed R and Rstudio, you will see Figure 1 on your machine.

Figure 1: First look

2.2.2 RStudio Installation - macOS

Installing R and RStudio on macOS is typically a straightforward process. It is recommended to proceed with the default settings and avoid making any changes unless you have a specific reason or are knowledgeable about the implications of modifying certain settings.

  • For RStudio, drag and drop the RStudio application to your applications folder.

If you have successfully installed R and Rstudio, you will see below on your machine.

2.2.2.1 Apple Command Line Tools

When launching RStudio for the first time on macOS, it may recommend downloading Apple Command Line Tools. It is highly beneficial to install this tool on your machine, as it offers various advantages. The installation process takes approximately 30 minutes.

Apple Command Line Tools Installation

2.3 Install Positron IDE

Positron IDE provide an augmented VSCode for data science. It is currently in open beta and available to download from GitHub. To navigate, follow the link: https://github.com/posit-dev/positron/releases.

Caution

If your device is sufficiently old (+4 yrs), use RStudio IDE instead. Positron won’t run smoothly on older OS versions.

Click Assets to open up the dropdown menu and download proper installer for your OS.

  • UserSetup.exe file for Windows users

  • .dmg file for MacOS

  • .deb for linux

If you have successfully installed R and Positron, you will see below:

3 Git Installation

3.1 Git for windows

  1. Search “Git Bash” on the search engine and go to Git Bash for Windows https://git-scm.com/downloads.

  1. Download for Windows and install, following the default settings.
  2. Open Git Bash and type below
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  1. Check if configuration is set:
# Lookout for user.name and user.email
git config --list 
  1. Path setup for R / Rscript location
  • You should find where the R is installed. The default location look as: (if your R version is 4.2.2)

  • /c/Program\ Files/R/R-4.2.2/bin

# Correctly specify the R version in the path
echo 'export PATH="$PATH:/c/Program\ Files/R/R-4.2.2/bin"' >> ~/.bashrc
source ~/.bashrc

3.2 Git for macOS

  1. Check if Xcode command line tools is not installed (if installed, skip)
  • Open terminal
xcode-select --install
  • It could take 30 minutes to install

Apple Command Line Tools Installation
  1. Configure git, your name and email address
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  1. Check if configuration is set:
# Lookout for user.name and user.email
git config --list 

4 Other Installations

4.1 Install WSL for Windows (Advanced)

  1. First you should check Turn Windows features on and off
  • Check Virtual Machine Platform

  • Check Windows Subsystem for Linux

  • You must Restart computer after this

  1. Launch command line prompt and run
wsl --install

by default, Ubuntu LTS 22 will be installed.

If you have already installed wsl previously, check status with wsl —-status and confirm you are using wsl2. If not, run:

wsl --update
wsl --set-default-version 2

For more details, check the link: https://learn.microsoft.com/en-us/windows/wsl/install

  1. From Ubuntu (bash), install git
sudo apt install git
  1. Configure git, your name and email address
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

4.2 Reticulate Python

4.2.1 Reticulate-Python in RStudio (Windows)

We will install Python using the R package called reticulate, which allows for seamless integration of R and Python within the RStudio IDE.

Please execute the following commands in your R console.

install.packages('reticulate')
library(reticulate)
install_miniconda()

If the installation mentioned above is successful, your Python installation is now complete.

  • For Windows machines, you will notice that Anaconda Prompt has been added to the start menu. You can utilize this prompt (terminal) going forward to install Python packages and manage your environments.

Windows: Anaconda Prompt generated on Start Menu

Windows: Miniconda Promopt

4.2.1.1 Setting Python Path for Windows (Optional)

If you already have Python installed on your machine, you can specify the Python path to use it instead of installing a new version. If this is the case, make sure to provide your own path. However, it is recommended to install Python from the reticulate package to avoid potential complications.

For Windows users, follow these steps:

  1. Copy paste below code on your command prompt. Then exit and re-launch RStudio.

echo RETICULATE_PYTHON = "%userprofile%\AppData\Local\r-miniconda\envs\r-reticulate\python.exe" >> %userprofile%\Documents\.Renviron

  1. Then open a python script (.py) and run the empty code to activate python console.
  2. Then Go to Tools > Global Options and set python interpreter for RStudio. Make sure you are choosing python from conda environments and choose python under r-reticulate environment as below.

RStudio Python Interpreter setup

4.2.2 Reticulate-Python in RStudio (MacOS)

To install Python and enable seamless integration with R in the RStudio IDE, we will use the R package called reticulate.

Please run the following command in your R console:

install.packages('reticulate')
library(reticulate)
install_miniconda()
  • On macOS, the operating system already includes a built-in Python 3 installation that comes with Apple Command Line Tools. However, we will not be using this Python installation. Instead, we will utilize the Miniconda Python installation that was installed from above steps.

  • To use conda in the Terminal on macOS, it needs to be activated and initialized. Follow these steps by copying and pasting the commands below into your Terminal, one line at a time:

  • Tip: You can access Terminal from your Launchpad or use the spotlight search by pressing Cmd + Spacebar and searching for Terminal.

4.2.2.1 Step 1

source ~/Library/r-miniconda/bin/activate
  • If you’re using Apple Silicon Mac, then name of miniconda directory will be slightly different. Use this code instead of above if this being the case.
source ~/Library/r-miniconda-arm64/bin/activate # use this command instead if Apple silicon

4.2.2.2 Step 2

Then, type command below. This will initialize conda on your zsh.

conda init zsh
  • If you are using bash as your default shell, then use conda init bash. Your shell name is on the top of the terminal.

  • If you see (base) on the left of your prompt, then your Terminal is set for conda!

MacOS: Anaconda Prompt generated

4.2.2.3 Set Python Path for Mac (Optional)

Please copy and paste the following command into your terminal. This command will generate the settings file. After executing the command, exit and relaunch RStudio.

echo 'RETICULATE_PYTHON="~/Library/r-miniconda/envs/r-reticulate/bin/python"' >> ~/.Renviron

4.3 Install Python (Vanilla)

There are numerous ways to download and install python, perhaps you already have python installed on your computer (e.g., MacOS / Linux).

I introduce the basic way. Navigate to Python webpage and download python installer, then install.

If you are on Mac and if you have already installed Apple command-line tools (if you are familiar with the below), you can skip Python installation.

Apple Command Line Tools Installation

4.4 Install Quarto

To install Quarto, Navigate to Quarto.org and download installer.

5 Troubleshooting

If, for any reason, the Python installation process described earlier does not work as expected, please refer to this section for troubleshooting steps. This section will be regularly updated with the latest information to assist you in resolving any installation issues.

5.1 Download time limit


If you are experiencing issues with R automatically disconnecting when downloading due to a slow or weak internet connection, you can apply a temporary fix by changing the timeout setting in R.

To do this, simply type the following command in your R console:

options(timeout=500) # setting timeout for 500 secs

To permanently change the internet timeout setting, follow the instructions based on your operating system:

For Windows, open the Command Prompt and execute the following command:

echo R_DEFAULT_INTERNET_TIMEOUT=500 >> %userprofile%\Documents\.Renviron

For macOS, open the Terminal and execute the following command:

echo "R_DEFAULT_INTERNET_TIMEOUT=500" >> ~/.Renviron

This command appends the specified configuration to your .Renviron file located in the Documents folder of your user profile, setting the internet timeout to 500 seconds.

5.2 Windows

5.2.1 Error installing miniconda due to whitespace

In windows, it is reported that when username has blank space such as "Ryan Gonzalez" , miniconda is not installed on the default path. In this case, user has to specify a different folder address where there is no blank space, such as C:\r-miniconda.

To do this, on R Prompt type this command.

reticulate::install_miniconda("C:\r-miniconda")

Then set python path for R Environment by typing below on command prompt.

echo RETICULATE_PYTHON = "C:\r-miniconda\envs\r-reticulate\python.exe" >> %userprofile%\Documents\.Renviron


5.2.2 Manually Setting up Anaconda Prompt

If anaconda prompt is not installed on your machine, we can manually add the prompt.

  • Create a shortcut (i.e. symlink) for command prompt on your desktop.

  • Go to its properties and change target as

    %windir%\System32\\cmd.exe "/K" %userprofile%\AppData\Local\R-MINI\~1\Scripts\activate.bat

  • change Start in as

    %HOMEPATH%

  • and paste the shortcut to a folder with proper folder name located at

    %userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs



5.3 MacOS

5.3.1 Reticulate path setting

Sometimes Rstudio does not locate the miniconda path automatically. In this case, user can manually set the path for Python and can tell R where it is installed.

5.3.2 Apple Silicon models

If miniconda installed by reticulate is x86_64 architecture, below error message will show:

    Error in py_initialize(config$python, config$libpython, config$pythonhome,  :

    : no suitable image found.  Did find:

    : mach-o, but wrong architecture

Instead, miniforge offers arm64(Apple Silicon) architecture, so I’m introducing this miniforge workaround.

  1. Go to Miniforge3 webpage and download Miniforge3-MacOSX-arm64.

It will be downloaded in the ~/Downloads.

  1. Then type below command on your terminal to install

    zsh \~/Downloads/Miniforge3-MacOSX-arm64.sh

miniforge3 will be installed on ~/miniforge3 by default. Answer yes to agree and another yes for initiating conda.

  1. Then, let’s make a new virtual environment1 called r-reticulate for consistency. Type below on your terminal:

conda create -n r-reticulate python


  1. Next, let’s set python path such that RStudio may find the path where miniforge3 is installed. Type below command on terminal:

echo 'RETICULATE_PYTHON="\~/miniforge3/envs/r-reticulate/bin/python"' >> ~/.Renviron

Now you’re ready to go.



5.4 Installing python packages

To install Python packages in R using the reticulate package, you have two convenient options: py_install() and conda_install() functions.

conda_install('r-reticulate','pandas') # install pandas package to our reticulated python environment
py_install('pandas') # will install pandas to reticulated python by default

The virtual environment of python we will be using is r-reticulate, which is a python env that is connected to the RStudio. We will be installing python packages on this environment.

To achieve this, we should activate our conda environment as r-reticulate by using command below on conda prompt.

conda activate r-reticulate

then let’s install python packages using command pip install. Or you can use conda install alternatively. Below packages are what we will be using throughout the semester.

pip install pandas numpy scipy numpy-financial xlwings mplfinance yfinance matplotlib jupyterlab

5.5 data.table Installation

The default installation (a.k.a. pre-compiled, or (compiled) binary version) of data.table package from CRAN for mac is not built with openMP-enabled compiler. This installation still works, but it forces to use 1 thread, which can limit its performance.

To install the package properly, you will need to compile it on your own mac, with openMP-enabled C compiler.

  1. Install homebrew to install C compiler for your mac.
  • Go to https://brew.sh and install homebrew by copy-pasting code on your terminal. It will require your mac password to install.

  • If homebrew asks you to run commands as next step, follow them. At this moment of writing, brew suggests to run below codes to add Homebrew to your PATH. If this is not done, it will not understand brew command.

    Homebrew next step

    Following the next step
  1. We will install some scaffolding packages and gcc as our compiler. Run brew install gcc pkg-config on your terminal.

  1. Check out the version of gcc installed. Type brew info gcc. At the moment of writing, the current gcc stable version is 13.1.0 (or simply 13).

  2. Now we have to tell R to use this gcc for package compilation. Type mkdir ~/.R and nano ~/.R/Makevars on your terminal (Case sensitive! Make sure to type correctly, or copy paste). If successful, you will see a text editor. This is needed so that you may not accidentally create an .rtf text file.

  3. Copy paste below code on your terminal, and save it. Note that GCC_VER is set to 13 as we verified in step 3. If the version number differs, set it accordingly. Exit by hitting ^X and save the file.

  • ARM based macs
LOC = /opt/homebrew
#/opt/homebrew for ARM Macs (M1 and its successors)

GCC_VER=13
CC=$(LOC)/bin/gcc-$(GCC_VER) -fopenmp
CXX=$(LOC)/bin/g++-$(GCC_VER) -fopenmp
CXX11=$(LOC)/bin/g++-$(GCC_VER) -fopenmp # for fst package

CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS += -L$(LOC)/opt/libomp/lib -lomp
CPPFLAGS += -I$(LOC)/opt/libomp/include -fopenmp
  • Intel based macs
LOC = /usr/local

#/usr/local for Intel Macs

GCC_VER=13
CC=$(LOC)/bin/gcc-$(GCC_VER) -fopenmp
CXX=$(LOC)/bin/g++-$(GCC_VER) -fopenmp
CXX11=$(LOC)/bin/g++-$(GCC_VER) -fopenmp # for fst package

CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath, -I$(LOC)/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include

  1. Close RStudio and open it again. Then install data.table package with following code.
install.packages("data.table", type = "source",
    repos = "https://Rdatatable.gitlab.io/data.table")
  1. To confirm a successful installation, when you load data.table, you will see it is using half of your number of cores as default.




6 Uninstall

6.1 macOS R uninstallation

Below code completely removes R, RStudio and miniconda (reticulate) installed on your computer. Use it when you’d like to remove and reinstall R/RStudio. You’ll need to provide password on the prompt.

sudo rm -rf /Applications/R.app
sudo rm -rf /Applications/RStudio.app
sudo rm -rf /Library/Frameworks/R.framework
sudo rm -rf /Library/Saved Application state/org.rstudio*
sudo rm -rf ~/.config/rstudio
sudo rm /usr/local/bin/{R,Rscript}
sudo rm /private/var/db/receipts/org.R-project*
sudo rm /private/var/db/receipts/org.r-project*
sudo rm /private/var/db/receipts/org.rstudio*

rm -rf ~/Library/Application Support/R
rm -rf ~/.Renviron

conda deactivate
sudo rm -rf ~/Library/r-miniconda/
rm -rf ~/.zshrc
rm -rf ~/.bashrc

7 Option settings

7.1 RStudio Workspace setup

Usually it is not recommended to load workspace in the beginning of the session, so let’s turn off this feature. Uncheck Restore .RData into workspace at startup and set Never to save workspace to .RData on exit.

Workspace setup

7.2 RStudio Terminal setup for MacOS

  • For MacOS, match the default shell (zsh) as RStudio’s default.

Change default shell for MacOS

Footnotes

  1. In simple terms, a virtual environment is like creating a copy of your Python installation to preserve the original setup.↩︎